package tcards.android;

import java.text.Format;
import java.text.SimpleDateFormat;

import models.UpdateHistory;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;

public class UpdateEntry extends LinearLayout {
	private TextDisplay mDate;
	private TextDisplay mOldStatus;
	private TextDisplay mNewStatus;
	private Button      mDelete;
	private TextField   mETR;
	private TextField   mLocation;
	private MultiField  mNotes;
	
	public UpdateEntry(Context context) {
		this(context, null, 0);
	}

	public UpdateEntry(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public UpdateEntry(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		LayoutInflater li = LayoutInflater.from(context);
		addView(li.inflate(R.layout.update_entry, this, false));
		
		mDate      = (TextDisplay) findViewById(R.id.entryDateDisplay);
		mOldStatus = (TextDisplay) findViewById(R.id.oldStatusDisplay);
		mNewStatus = (TextDisplay) findViewById(R.id.newStatusDisplay);
		mDelete    = (Button)      findViewById(R.id.entryDeleteButton);
		mETR       = (TextField)   findViewById(R.id.etrField);
		mLocation  = (TextField)   findViewById(R.id.locationField);
		mNotes     = (MultiField)  findViewById(R.id.notesField);
	}
	
	public void setHistoryFields(UpdateHistory history) {
		history.setETR(mETR.getTextValue());
		history.setIncidentLocation(mLocation.getTextValue());
		history.setNotes(mNotes.getTextValue());
	}
	
	public void setListFields(UpdateHistory history) {
		Format dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mma");
		mDate.setTextValue(dateFormat.format(history.getUpdateDate()));
		mOldStatus.setTextValue(history.getOldStatus().toDisplayString());
		mNewStatus.setTextValue(history.getNewStatus().toDisplayString());
		mETR.setTextValue(history.getETR());
		mLocation.setTextValue(history.getIncidentLocation());
		mNotes.setTextValue(history.getNotes());
	}
	
	public TextDisplay getDate() {
		return mDate;
	}
	
	public TextDisplay getOldStatus() {
		return mOldStatus;
	}
	
	public TextDisplay getNewStatus() {
		return mNewStatus;
	}
	
	public void setDeleteOnClick(OnClickListener l) {
		mDelete.setOnClickListener(l);
	}
	
	public void setDeleteOnLongClick(OnLongClickListener l) {
		mDelete.setOnLongClickListener(l);
	}
	
	public TextField getETR() {
		return mETR;
	}
	
	public TextField getLocation() {
		return mLocation;
	}
	
	public MultiField getNotes() {
		return mNotes;
	}
}